/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.util;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by Red SKIOS.
 * User: Administrador
 * Date: 28-ene-2004
 * Time: 12:35:58
 */
public class Languages
{
    protected static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog( Languages.class.getName() );
	private Document document;

	public Languages(ServletContext context, String filename)
	{
		filename = "errormessages.xml";
		try
		{
			if (context.getResourceAsStream("/WEB-INF/"+filename)==null)
				throw new IllegalStateException("errormessages.xml not found in /WEB-INF");
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(context.getResourceAsStream("/WEB-INF/"+filename));
		}
		catch(Exception e)
		{
			context.log("ATCHUNG: Can't load languages: "+e.getMessage());
			//throw new IllegalStateException(e.getMessage());
		}
	}
	public Languages(Document document)
	{
		this.document = document;
	}
	public String[] getError(String languageid, String errorcode)
	{
		String sid = errorcode.substring(0,errorcode.length()-2);
		String eid = errorcode.substring(sid.length());
		NodeList langs = document.getElementsByTagName("languages").item(0).getChildNodes();

		Node lang = getNodeByCodeValue(langs,languageid);
		Node components = getNode(lang.getChildNodes(),"components");
		Node component = getNodeByCodeValue(components.getChildNodes(),sid);
		Node errors = getNode(component.getChildNodes(),"errors");
		Node error = getNodeByCodeValue(errors.getChildNodes(),eid);
		String[] data = new String[2];
		data[0] = getNode(component.getChildNodes(),"name").getFirstChild().getNodeValue();
		data[1] = getNode(error.getChildNodes(),"description").getFirstChild().getNodeValue();
        return data;

	}
	private Node getNodeByCodeValue(NodeList nodes, String value)
	{

		for(int n=0; n<nodes.getLength(); n++)
		{
			Node nl = nodes.item(n);
			for(int c=0; c<nl.getChildNodes().getLength(); c++)
			if (nl.getChildNodes().item(c).getNodeName().equals("code") && nl.getChildNodes().item(c).getFirstChild().getNodeValue().equals(value))
				return nl;
		}
		return null;
	}
	private Node getNode(NodeList nodes, String name)
	{
		for(int n=0; n<nodes.getLength(); n++)
		{

			if (nodes.item(n).getNodeName().equals(name))
				return nodes.item(n);
		}
		log.error("No Language Node for name: "+name);
		return null;
	}
	public static void main(String[] args)
	{
		try
		{
			File f = new File("/var/lib/webapps/tuportalWeb/WEB-INF/errormessages.xml");
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(f));
			Languages l = new Languages(doc);
			String[] data = l.getError("es","101");
			log.info("Ended");
		} catch (ParserConfigurationException e)
		{
			e.printStackTrace();  //To change body of catch statement use Options | File Templates.
		} catch (FactoryConfigurationError factoryConfigurationError)
		{
			factoryConfigurationError.printStackTrace();  //To change body of catch statement use Options | File Templates.
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();  //To change body of catch statement use Options | File Templates.
		} catch (SAXException e)
		{
			e.printStackTrace();  //To change body of catch statement use Options | File Templates.
		} catch (IOException e)
		{
			e.printStackTrace();  //To change body of catch statement use Options | File Templates.
		}
	}

}
